<template>
    <div class="pagination">
        <button :disabled="pageNoInt === 1||pageNoInt === 0" @click="changePage(pageNoInt - 1)">
            上一页
        </button>
        <button v-if="start > 1" @click="changePage(1)">1</button>
        <button v-if="start >= 3">···</button>

        <!-- 中间部分 -->
        <button
            v-for="(page, index) in end - start + 1"
            :key="index"
            :class="{ active: pageNoInt == start + index }"
            @click="changePage(start + index)"
        >
            {{ index + start }}
        </button>

        <button v-if="end <= totalPage - 2">···</button>
        <button v-if="end < totalPage" @click="changePage(totalPage)">
            {{ totalPage }}
        </button>
        <button
            :disabled="pageNoInt === totalPage||totalPage ===0"
            @click="changePage(pageNoInt + 1)"
        >
            下一页
        </button>

        <button style="margin-left: 30px">共 {{ total }} 条</button>
    </div>
</template>

<script>
export default {
    props: ["pageNo", "pageSize", "total", "continues"],
    name: "Pagination",
    computed: {
        continuesInt() {
            return parseInt(this.continues);
        },
        pageNoInt() {
            return parseInt(this.pageNo);
        },
        pageSizeInt() {
            return parseInt(this.pageSize);
        },
        totalInt() {
            return parseInt(this.total);
        },
        // 计算分页器的页数
        totalPage() {
            return Math.ceil(this.totalInt / this.pageSizeInt);
            // Mtah.ceil向上取整
            // Math.floor向下取整
        },
        // 计算连续页码的起始 数字与结束数字[连续页码数字最少是5]
        startNumAndEndNum() {
            let { continuesInt, totalPage, pageNoInt } = this;
            // 定义两个遍历存储起始与结束数字
            let start = 0,
                end = 0;
            if (continuesInt > totalPage) {
                // 如果总共页数不够五页呢？
                start = 1;
                end = totalPage;
            } else {
                start = pageNoInt - Math.floor(continuesInt / 2);
                end = parseInt(pageNoInt) + Math.floor(continuesInt / 2);
            }
            // 开始页码不能小于1，结束页码不能大于totalPage

            if (start < 1) {
                start = 1;
                end = continuesInt;
            } else if (end > totalPage) {
                end = totalPage;
                start = end - continuesInt + 1;
            }
            return { start, end };
        },
        start() {
            return this.startNumAndEndNum.start;
        },
        end() {
            return this.startNumAndEndNum.end;
        },
    },
    methods: {
        // 修改当前页面
        changePage(index) {
            // 传递当前的页面到父组件
            this.$emit("changePage", index);
        },
    },
};
</script>

<style lang="less" scoped>
.pagination {
    text-align: center;
    button {
        margin: 0 5px;
        background-color: #f4f4f5;
        color: #606266;
        outline: none;
        border-radius: 2px;
        padding: 0 4px;
        vertical-align: top;
        display: inline-block;
        font-size: 13px;
        min-width: 35.5px;
        height: 28px;
        line-height: 28px;
        cursor: pointer;
        box-sizing: border-box;
        text-align: center;
        border: 0;

        &[disabled] {
            color: #c0c4cc;
            cursor: not-allowed;
        }

        &.active {
            cursor: not-allowed;
            background-color: #409eff;
            color: #fff;
        }
    }
}
</style>